import Router from 'koa-router'
import User from '../model/User'

const userRouter = new Router()

userRouter.prefix('/user')

userRouter.get('/addUser', async (ctx) => {
  const user = new User({ name: '小红' })
  try {
    const res = await user.save()
    console.log(res)
    // eslint-disable-next-line no-empty
    ctx.body = {
      code: 200,
      data: res,
    }
  } catch (e) {
    ctx.body = {
      error: e,
    }
  }
})

userRouter.get('/users', async (ctx, next) => {
  const users = await User.find()
  console.log('users', users)
  ctx.body = {
    code: 200,
    data: users,
  }
})

userRouter.get('/setCookie', async (ctx, next) => {
  ctx.cookies.set('cid', 'ddddd', {
    // domain: 'localhost', // 写cookie所在的域名
    path: '/', // 写cookie所在的路径
    maxAge: 10 * 60 * 1000, // cookie有效时长
    httpOnly: false, // 是否只用于http请求中获取
    // overwrite: false, // 是否允许重写
  })
  ctx.body = {
    code: 200,
    data: 'ok',
  }
})
userRouter.get('/getCookie', async (ctx, next) => {
  const cookie1 = ctx.cookies.get('name')
  const cookie2 = ctx.cookies.get('age')
  console.log('nameCookie', cookie1)
  console.log('ageCookie', cookie2)
  ctx.body = {
    code: 200,
    data: 'ok',
  }
})

export = userRouter
